package Alert;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import javax.print.attribute.AttributeSet;
import javax.swing.ComboBoxEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
import mysql.connect;
import STOCK.STOCK;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddAlert.java
 *
 * Created on Sep 10, 2011, 7:27:53 PM
 */
/**
 *
 * @author Anatomy
 */

 

public class ChkAlert extends javax.swing.JDialog {
    private int count;
    public String idchk;

    /** Creates new form AddAlert */
    public ChkAlert(java.awt.Frame parent, boolean modal) {
        
        super(parent, modal);
        initComponents();
        Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();
    int screenHeight = screenSize.height;
    int screenWidth = screenSize.width;
   // setSize(screenWidth / 2, screenHeight / 2);
    setLocation(screenWidth / 4, screenHeight / 4);
   
     setTitle("Set Alert");
        // add data
        // txtAddSysbol.add(new JScrollPane(list)); 
  
  

    }

    public String getIdchk() {
        return idchk;
    }

    public void setIdchk(String idchk) {
        this.idchk = idchk;
    }
    

    public ChkAlert() {
        //throw new UnsupportedOperationException("Not yet implemented");
    }
    public void sevi(){
        setVisible(false);
    }
  
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCheckBox1 = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Alert");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jCheckBox1.setText("ปิดการเตือน");
        jCheckBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCheckBox1ItemStateChanged(evt);
            }
        });

        jButton1.setText("ตกลง");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBox1)
                            .addComponent(jButton1))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            String sqlsec="SELECT * FROM `stock_ alert`";
            connect dbcon=new connect(); 
             dbcon.rs= dbcon.stmt.executeQuery(sqlsec);
              dbcon.rs.next();
            String chk=dbcon.rs.getString("state"); 
            if(chk.equals("0")){
                jCheckBox1.setSelected(true);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ChkAlert.class.getName()).log(Level.SEVERE, null, ex);
        }
       
}//GEN-LAST:event_formWindowOpened

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
   sevi();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jCheckBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBox1ItemStateChanged
     if(jCheckBox1.isSelected()){
        
            connect dbcon=new connect(); 

                      String    sqlsec="UPDATE `stock_ alert` SET `state` = '0' WHERE `stock_ alert`.`stockname` ='"+getIdchk()+"' ;";
            try {
              dbcon.stmt.executeUpdate(sqlsec);
            } catch (SQLException ex) {
                Logger.getLogger(ChkAlert.class.getName()).log(Level.SEVERE, null, ex);
            }
                     
                       dbcon.CloseDB();
     }else{
            connect dbcon=new connect(); 

                      String    sqlsec="UPDATE `stock_ alert` SET `state` = '1' WHERE `stock_ alert`.`stockname` ='"+getIdchk()+"' ;";
            try {
              dbcon.stmt.executeUpdate(sqlsec);
            } catch (SQLException ex) {
                Logger.getLogger(ChkAlert.class.getName()).log(Level.SEVERE, null, ex);
            }
                     
                       dbcon.CloseDB();
     }
    }//GEN-LAST:event_jCheckBox1ItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
       
             Properties props = new Properties(); 
             props.put("logoString", "SEDSS");
             com.jtattoo.plaf.acryl.AcrylLookAndFeel.setCurrentTheme(props);
                
        
            UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
            
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ChkAlert dialog = new ChkAlert(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
              
         
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    public javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}

